<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
  <head>
  <title>New Document</title>
  <script id="shader-fs" type="x-shader/x-fragment">
    precision mediump float;
    varying vec4 vColor;

    void main(void) {
        gl_FragColor = vColor;
    }
</script>
<script id="shader-vs" type="x-shader/x-vertex">
	attribute vec3 aVertexPosition;
	attribute vec4 aVertexColor;
	
	uniform mat4 uMVMatrix;
	uniform mat4 uPMatrix;
	
	varying vec4 vColor;
	
	void main(void) {
		gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
		vColor = aVertexColor;
	}
</script>
<script>
	function start() {
		var canvas = document.getElementById("glcanvas");  
		initGL(canvas);        
		initShaders();        
		initBuffers(); 
	
		gl.clearColor(0.0, 0.0, 0.0, 1.0);        
		gl.enable(gl.DEPTH_TEST); 
	
		drawScene2();    
	}

	var gl;    
	function initGL(canvas) {  
		gl=null;      
		try {
			// Try to grab the standard context. If it fails, fallback to experimental.
			gl = canvas.getContext("webgl") || canvas.getContext("experimental-webgl");
		}catch(e) {
			alert("initGL :"+e);
		}
		// If we don't have a GL context, give up now
		if (!gl) {
			alert("Unable to initialize WebGL. Your browser may not support it.");
		}
	}

	var shaderProgram;

    function initShaders() {
        var fragmentShader = getShader(gl, "shader-fs");
        var vertexShader = getShader(gl, "shader-vs");

        shaderProgram = gl.createProgram();
        gl.attachShader(shaderProgram, vertexShader);
        gl.attachShader(shaderProgram, fragmentShader);
        gl.linkProgram(shaderProgram);

        if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
            alert("Could not initialise shaders");
        }

        gl.useProgram(shaderProgram);

        shaderProgram.vertexPositionAttribute = gl.getAttribLocation(shaderProgram, "aVertexPosition");
        gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute);

        shaderProgram.vertexColorAttribute = gl.getAttribLocation(shaderProgram, "aVertexColor");
        gl.enableVertexAttribArray(shaderProgram.vertexColorAttribute);

        shaderProgram.pMatrixUniform = gl.getUniformLocation(shaderProgram, "uPMatrix");
        shaderProgram.mvMatrixUniform = gl.getUniformLocation(shaderProgram, "uMVMatrix");
    }

	function getShader(gl, id) {
		var shaderScript, theSource, currentChild, shader;
		
		shaderScript = document.getElementById(id);    
		if (!shaderScript) {
			return null;
		}
		
		theSource = "";
		currentChild = shaderScript.firstChild;    
		while(currentChild) {
			if (currentChild.nodeType == currentChild.TEXT_NODE) {
				theSource += currentChild.textContent;
			}
			
			currentChild = currentChild.nextSibling;
		}
		if (shaderScript.type == "x-shader/x-fragment") {
		  shader = gl.createShader(gl.FRAGMENT_SHADER);
		} else if (shaderScript.type == "x-shader/x-vertex") {
		  shader = gl.createShader(gl.VERTEX_SHADER);
		} else {
		 // Unknown shader type
		 return null;
		}
	   gl.shaderSource(shader, theSource);
		
	   // Compile the shader program
	   gl.compileShader(shader);  
		
	   // See if it compiled successfully
	   if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {  
		  alert("An error occurred compiling the shaders: " + gl.getShaderInfoLog(shader));  
		  return null;  
	   }
		
	   return shader;
	}

	var triangleVertexPositionBuffer;
    var triangleVertexColorBuffer;

    function initBuffers() {
        triangleVertexPositionBuffer = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexPositionBuffer);
        var vertices = [
             0.0,  1.0,  0.0,
            -1.0, -1.0,  0.0,
             1.0, -1.0,  0.0
        ];
        gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
        triangleVertexPositionBuffer.itemSize = 3;
        triangleVertexPositionBuffer.numItems = 3;

        triangleVertexColorBuffer = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexColorBuffer);
        var colors = [
            1.0, 0.0, 0.0, 1.0,
            0.0, 1.0, 0.0, 1.0,
            0.0, 0.0, 1.0, 1.0
        ];
        gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(colors), gl.STATIC_DRAW);
        triangleVertexColorBuffer.itemSize = 4;
        triangleVertexColorBuffer.numItems = 3;
    }

	function drawScene() {
		gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight);
		gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
	
		pMatrix = okMat4Proj(45.0, gl.viewportWidth / gl.viewportHeight, 0.1, 100.0);
		mvMatrix = okMat4Trans(-1.5, 0.0, -7.0);
	
		gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexPositionBuffer);
		gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, triangleVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
	
		gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexColorBuffer);
		gl.vertexAttribPointer(shaderProgram.vertexColorAttribute, triangleVertexColorBuffer.itemSize, gl.FLOAT, false, 0, 0);
	
		setMatrixUniforms();
		gl.drawArrays(gl.TRIANGLES, 0, triangleVertexPositionBuffer.numItems);
	}
	
	function drawScene2() {
		gl.clear(gl.COLOR_BUFFER_BIT |gl.DEPTH_BUFFER_BIT);
		perspective(45, 1.0, 0.1, 100.0);
	}
  </script>
  </head>

  <body onLoad="start()">
  <canvas id="glcanvas" width="640" height="480"> Your browser doesn't appear to support the HTML5 canvas element. </canvas>
</body>
</html>
